package com.srit.yoggsaron.swagger;
         

import java.util.Arrays;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2  // implements WebMvcConfigurer
{
	 private static final String SPLITOR = ";";
	@Bean
	public Docket createRestApi() {
		
		
		StringBuffer sb=new StringBuffer();
		sb.append( "com.srit.yoggsaron.controller".trim() ).append( SPLITOR );
		
		
		java.util.List<ResponseMessage> resMsgList = Arrays.asList(
				new ResponseMessageBuilder().code(200).message("成功！").build(),
				new ResponseMessageBuilder().code(-1).message("失败！").build(),
				new ResponseMessageBuilder().code(401).message("参数校验错误！").build(),
				new ResponseMessageBuilder().code(403).message("没有权限操作，请后台添加相应权限！").build(),
				new ResponseMessageBuilder().code(500).message("服务器内部异常，请稍后重试！").build(),
				new ResponseMessageBuilder().code(501).message("请登录！").build());

		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
//				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				
				.apis(basePackage(  sb.toString() ))
				
				.paths(PathSelectors.any())
				.build()
				.globalResponseMessage(RequestMethod.GET, resMsgList)
				.globalResponseMessage(RequestMethod.POST, resMsgList)
				.globalResponseMessage(RequestMethod.PUT, resMsgList)
				.globalResponseMessage(RequestMethod.DELETE, resMsgList);
		
		
		
		
		//====================
		
		
//		return new Docket(DocumentationType.SWAGGER_2)
//				.apiInfo(apiInfo())
//				.select()
//				.apis(basePackage(  sb.toString() ))
//				.paths(PathSelectors.any())
//				.build();
	}
	
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("V5小助手功接口")
				.description("简单优雅的风格 ")
//				.termsOfServiceUrl("http://blog.csdn.net/saytime")
				.version("1.0")
				.build();
	}
	
	/**
     * 重写basePackage方法，使能够实现多包访问，复制贴上去
     * @author  caiqi
     * @date 2019/7/26
     * @param [basePackage]
     * @return com.google.common.base.Predicate<springfox.documentation.RequestHandler>
     */
 public static Predicate<RequestHandler> basePackage(final String basePackage) {
     return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
 }

 private static Function<Class<?>, Boolean> handlerPackage(final String basePackage)     {
     return input -> {
         // 循环判断匹配
         for (String strPackage : basePackage.split(SPLITOR)) {
             boolean isMatch = input.getPackage().getName().startsWith(strPackage);
             if (isMatch) {
                 return true;
             }
         }
         return false;
     };
 }

 @SuppressWarnings("deprecation")
private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
     return Optional.fromNullable(input.declaringClass());
 }
}

